package com.io.example.demoparent.mq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;


public class Consumer {
    public static final  String QUEUE_NAME="hello";

          public static  void  put() throws IOException, TimeoutException {
              //创建一个链接工厂
              ConnectionFactory factory=new ConnectionFactory();
              //工厂ip 链接Rabbit队列
              factory.setHost("192.168.164.131");
              //端口
//        factory.setPort(15672);
              //用户名
              factory.setUsername("guest");
              //密码
              factory.setPassword("guest");
              //创建一个信道
              Connection connection = factory.newConnection( );
              //获取信道
              Channel channel = connection.createChannel( );
              /*
               *   生产一个队列
               *   1.队列名称
               *   2.队列里面的消息是否实持久化，默认情况储存在内存中
               *   3.该队列是否提供一个消费者，是否进行消息共享
               *   4.之是否自动删除
               *   5.其他
               */
              channel.queueDeclare(QUEUE_NAME,false,false,false,null);

              /*
               *   消费者消费
               *  1.消费队列
               *  2.消费成功后是否自定应答
               *  3.消费为成功消费的回调
               *  4.消费者取消消费回调
               *
               */
              channel.basicConsume(QUEUE_NAME, true, new DeliverCallback( ) {
                  @Override
                  public void handle(String s, Delivery delivery) throws IOException {
                      System.out.println("delivery = " + new String(delivery.getBody( )));
                  }
              }, new CancelCallback( ) {
                  @Override
                  public void handle(String s) throws IOException {
                      System.out.println("s = " + s);
                  }
              });
          }


    public static void main(String[] args) throws IOException, TimeoutException {
        put();
    }
}
